﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.EntityClient;
using CongregacaoOs.Data;

namespace CongregacaoOs.Business
{
    [System.ComponentModel.DataObject]
    public class ccSetorCargosBus
    {        

            [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
            public bool SetorCargosSaveOrUpdate(ccSetorCargos aSetorCargos)
            {
                if (aSetorCargos.idSetor == 0) { throw new Exception("O Setor não foi informado"); }

                using (EntityConnection conn = new EntityConnection("name = CcbOrdemServico"))
                {
                    try
                    {
                        using (EntityCommand cmd = conn.CreateCommand())
                        {
                            cmd.CommandText = "CcbOrdemServico.ccSetorCargosSave";
                            cmd.CommandType = System.Data.CommandType.StoredProcedure;

                            EntityParameter param;

                            param = cmd.Parameters.Add("IDSETORCARGO", DbType.Int32);
                            param.Direction = ParameterDirection.Input;
                            if (aSetorCargos.idSetorCargos != 0)
                                param.Value = aSetorCargos.idSetorCargos;
                            else
                                param.Value = DBNull.Value;

                            param = cmd.Parameters.Add("IDSETOR", DbType.Int32);
                            param.Direction = ParameterDirection.Input;
                            if (aSetorCargos.idSetor != 0)
                                param.Value = aSetorCargos.idSetor;
                            else
                                param.Value = DBNull.Value;

                            param = cmd.Parameters.Add("IDCARGO", DbType.Int32);
                            param.Direction = ParameterDirection.Input;
                            if (aSetorCargos.idCargo != 0)
                                param.Value = aSetorCargos.idCargo;
                            else
                                param.Value = DBNull.Value;

                            using (conn) 
                            {
                                if(conn.State == ConnectionState.Closed)
                                    conn.Open();

                                cmd.ExecuteNonQuery();
                                return true;
                            }
                            
                            
                            
                            
                        }
                    }
                    catch (Exception)
                    {
                        return false;
                    }

                }
            }

            [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
            public List<ccSetorCargosSelect_Result> SetorCargosSelect(int aIdSetor = 0)
            {
                using (EntityConnection conn = new EntityConnection("name = CcbOrdemServico"))
                {                    
                    try
                    {
                        using (EntityCommand cmd = conn.CreateCommand())
                        {
                            cmd.CommandText = "CcbOrdemServico.ccSetorCargosSelect1";
                            cmd.CommandType = System.Data.CommandType.StoredProcedure;

                            EntityParameter param;

                            param = cmd.Parameters.Add("IDSETOR", DbType.Int32);
                            param.Direction = ParameterDirection.Input;
                            if (aIdSetor != 0)
                                param.Value = aIdSetor;
                            else
                                param.Value = DBNull.Value;



                            using (conn)
                            {
                                if(conn.State == ConnectionState.Closed)
                                    conn.Open();
                                using (EntityDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
                                {

                                    List<ccSetorCargosSelect_Result> _result = new List<ccSetorCargosSelect_Result>();
                                    while (reader.Read())
                                    {
                                        ccSetorCargosSelect_Result _SetorCargos = new ccSetorCargosSelect_Result();
                                        _SetorCargos.idCargo = reader.IsDBNull(reader.GetOrdinal("idCargo")) == false ? reader.GetInt32(reader.GetOrdinal("idCargo")) : 0;
                                        _SetorCargos.Cargo = reader.IsDBNull(reader.GetOrdinal("Cargo")) == false ? reader.GetString(reader.GetOrdinal("Cargo")) : "";
                                        _result.Add(_SetorCargos);
                                    }
                                    


                                    return _result;
                                }
                            }


                        }
                    }
                    catch (Exception)
                    {
                        return null;
                    }

                }
            }


            [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Delete, true)]
            public void SetorCargosDeleteBySetor(int aIdSetor)
            {
                try
                {
                    using (CcbOrdemServico objDB = new CcbOrdemServico())
                    {
                        objDB.ccSetorCargosDeleteBySetor(aIdSetor);
                    }
                }
                catch (System.Exception)
                {

                    throw;
                }
            }
        
    }
}
